/*
 * File: AirConditioner.c
 *
 * Code generated for Simulink model 'AirConditioner'.
 *
 * Model version                  : 1.3
 * Simulink Coder version         : 25.1 (R2025a) 21-Nov-2024
 * C/C++ source code generated on : Fri Jul 18 10:48:09 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "AirConditioner.h"
#include "rtwtypes.h"
#include "AirConditioner_private.h"

/* Named constants for Chart: '<Root>/AC_Enable' */
#define AirConditioner_IN_POWER_OFF    ((uint8_T)1U)
#define AirConditioner_IN_POWER_ON     ((uint8_T)2U)

/* Block states (default storage) */
DW_AirConditioner_T AirConditioner_DW;

/* External outputs (root outports fed by signals with default storage) */
ExtY_AirConditioner_T AirConditioner_Y;

/* Real-time model */
static RT_MODEL_AirConditioner_T AirConditioner_M_;
RT_MODEL_AirConditioner_T *const AirConditioner_M = &AirConditioner_M_;

/* Model step function */
void AirConditioner_step(void)
{
  /* local block i/o variables */
  real_T rtb_Switch;
  real_T tmp;

  /* FromWorkspace: '<S2>/fromWS_Signal 1' */
  {
    real_T *pDataValues = (real_T *)
      AirConditioner_DW.fromWS_Signal1_PWORK.DataPtr;
    real_T *pTimeValues = (real_T *)
      AirConditioner_DW.fromWS_Signal1_PWORK.TimePtr;
    int_T currTimeIndex = AirConditioner_DW.fromWS_Signal1_IWORK.PrevIndex;
    real_T t = ((AirConditioner_M->Timing.clockTick0) * 0.1);
    if (t >= pTimeValues[5]) {
      rtb_Switch = pDataValues[5];
    } else {
      /* Get index */
      if (t <= pTimeValues[0]) {
        currTimeIndex = 0;
      } else if (t >= pTimeValues[5]) {
        currTimeIndex = 4;
      } else {
        if (t < pTimeValues[currTimeIndex]) {
          while (t < pTimeValues[currTimeIndex]) {
            currTimeIndex--;
          }
        } else {
          while (t >= pTimeValues[currTimeIndex + 1]) {
            currTimeIndex++;
          }
        }
      }

      AirConditioner_DW.fromWS_Signal1_IWORK.PrevIndex = currTimeIndex;

      /* Post output */
      {
        real_T t1 = pTimeValues[currTimeIndex];
        real_T t2 = pTimeValues[currTimeIndex + 1];
        if (t1 == t2) {
          if (t < t1) {
            rtb_Switch = pDataValues[currTimeIndex];
          } else {
            rtb_Switch = pDataValues[currTimeIndex + 1];
          }
        } else {
          real_T f1 = (t2 - t) / (t2 - t1);
          real_T f2 = 1.0 - f1;
          real_T d1;
          real_T d2;
          int_T TimeIndex = currTimeIndex;
          d1 = pDataValues[TimeIndex];
          d2 = pDataValues[TimeIndex + 1];
          rtb_Switch = (real_T) rtInterpolate(d1, d2, f1, f2);
          pDataValues += 6;
        }
      }
    }
  }

  /* Chart: '<Root>/AC_Enable' */
  if (AirConditioner_DW.is_active_c3_AirConditioner == 0) {
    AirConditioner_DW.is_active_c3_AirConditioner = 1U;
    AirConditioner_DW.is_c3_AirConditioner = AirConditioner_IN_POWER_OFF;

    /* Switch: '<S3>/Switch' incorporates:
     *  Constant: '<S3>/Constant1'
     */
    tmp = 0.0;
  } else if (AirConditioner_DW.is_c3_AirConditioner ==
             AirConditioner_IN_POWER_OFF) {
    if ((rtb_Switch == 1.0) && (AirConditioner_Y.temp_act - 24.0 > 1.0)) {
      AirConditioner_DW.is_c3_AirConditioner = AirConditioner_IN_POWER_ON;

      /* Switch: '<S3>/Switch' incorporates:
       *  Constant: '<S3>/temp_drop'
       */
      tmp = -0.01;
    } else {
      /* Switch: '<S3>/Switch' incorporates:
       *  Constant: '<S3>/Constant1'
       */
      tmp = 0.0;
    }

    /* case IN_POWER_ON: */
  } else if ((rtb_Switch == 0.0) || (AirConditioner_Y.temp_act - 24.0 < -1.0)) {
    AirConditioner_DW.is_c3_AirConditioner = AirConditioner_IN_POWER_OFF;

    /* Switch: '<S3>/Switch' incorporates:
     *  Constant: '<S3>/Constant1'
     */
    tmp = 0.0;
  } else {
    /* Switch: '<S3>/Switch' incorporates:
     *  Constant: '<S3>/temp_drop'
     */
    tmp = -0.01;
  }

  /* End of Chart: '<Root>/AC_Enable' */

  /* Sum: '<S3>/Add1' incorporates:
   *  Constant: '<Root>/temp_env'
   *  Gain: '<S3>/Thermal Isolation'
   *  Sum: '<S3>/Add'
   *  Switch: '<S3>/Switch'
   *  UnitDelay: '<Root>/Unit Delay'
   *  UnitDelay: '<S3>/Unit Delay'
   */
  AirConditioner_Y.temp_act = ((35.0 - AirConditioner_DW.UnitDelay_DSTATE_b) *
    0.0002 + tmp) + AirConditioner_DW.UnitDelay_DSTATE_b;

  /* Update for UnitDelay: '<S3>/Unit Delay' incorporates:
   *  UnitDelay: '<Root>/Unit Delay'
   */
  AirConditioner_DW.UnitDelay_DSTATE_b = AirConditioner_Y.temp_act;

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 0.1, which is the step size
   * of the task. Size of "clockTick0" ensures timer will not overflow during the
   * application lifespan selected.
   */
  AirConditioner_M->Timing.clockTick0++;
}

/* Model initialize function */
void AirConditioner_initialize(void)
{
  /* Start for FromWorkspace: '<S2>/fromWS_Signal 1' */
  {
    static real_T pTimeValues0[] = { 0.0, 200.0, 200.0, 700.0, 700.0, 1000.0 } ;

    static real_T pDataValues0[] = { 0.0, 0.0, 1.0, 1.0, 0.0, 0.0 } ;

    AirConditioner_DW.fromWS_Signal1_PWORK.TimePtr = (void *) pTimeValues0;
    AirConditioner_DW.fromWS_Signal1_PWORK.DataPtr = (void *) pDataValues0;
    AirConditioner_DW.fromWS_Signal1_IWORK.PrevIndex = 0;
  }

  /* InitializeConditions for UnitDelay: '<S3>/Unit Delay' */
  AirConditioner_DW.UnitDelay_DSTATE_b = 32.0;
}

/* Model terminate function */
void AirConditioner_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
